<?php

defined( '_JEXEC' ) or die( 'Restricted access' );
jimport('joomla.application.component.controller');
 
class WrdControllerRedemption extends JControllerLegacy {
 	
	function __construct($cfg = array()) 
	{	
		parent::__construct($cfg);
	}		
		
	function redeemtion(){
		$mainframe = JFactory::getApplication();		
		$pid = JRequest::getInt('pid');		
		if($pid == 0)	{
			$mainframe->redirect('index.php?option=com_wrd&view=redemtion&layout=products','Invalid product');						return false;		
		}
					
		$user = JFactory::getUser();		
		
		if($user->id == 0){
			$mainframe->redirect('index.php#login','You need login first');	
			return false;		
		}				
		
		$db = JFactory::getDbo();				 
		
		//get Product detail		
		$query = 'SELECT * FROM #__wrd_products WHERE id = '.$pid;		
		$db->setQuery($query);		
		$rs = $db->loadObject();		
		
		if($rs->quantity == 0)		
		{
			$mainframe->redirect('index.php?option=com_wrd&view=redemtion&layout=products','Out of stock');		
			return false;		
		}		
		
		$mainframe->redirect('index.php?option=com_wrd&view=redemption&layout=confirm&id='.$rs->id.'&Itemid=127');	
	}	
	
	public function pageswitch(){
		$mainframe = JFactory::getApplication() ;		
		$post = JRequest::get($post) ;		
		$db = JFactory::getDbo();		
		
		if($post['switch_value']=='Yes'){
			$vercode = $this->random_letters() ;			
			$mainframe->setUserState("flag_here", "$vercode") ;			
			$mainframe->setUserState("confirm_number", $vercode) ;						
						
			//get user			
			$user = JFactory::getUser();						
			//get product			
			$query = 'SELECT * FROM #__wrd_products WHERE id = '.$post['pid'];			
			$db->setQuery($query);			
			$rs = $db->loadObject();			
			//get point detail			
			$point = WrdHelper::getPoints($user->id);			
			if($post['type_redeem'] == 'redeem_by_point'){
				$t_point = $rs->point ;				
				$t_cash = 0 ;				
				$point_redeem = $rs->point ;			
			}else{
				$t_point = $rs->pointx ;				
				$t_cash = $rs->cash ;				
				$point_redeem = $rs->pointx ;			
			}			
			
			if($point >= $point_redeem)			
			{
				//updated point				
				$obj = new stdClass();				
				$obj->id = null;				
				$obj->user_id = $user->id;				
				$obj->transaction = $rs->id;				
				$obj->type = 0;				
				$obj->point = -$point_redeem;				
				$obj->expire = '0000-00-00 00:00:00';				
				$obj->point = -$point_redeem;				
				$obj->des = 'Redemption';				
				$obj->created_id = $user->id ;				
				$obj->created_date = date('Y-m-d') ;				
				$obj->modify_id = '';				
				$obj->modify_date = '';			
				$obj->flagConvert = 1;						
				$db->insertObject('#__wrd_point', $obj);				
				
				unset($obj);				
				$temp = $db->insertid() ;				
				//process gift				
				$obj = new stdClass();				
				$obj->id = null;				
				$obj->user_id = $user->id;				
				$obj->created_id = $user->id;				
				$obj->product_id = $rs->id;				
				$obj->exchange_date = date("Y-m-d H:i:s") ;				
				$obj->approved_date = '0000-00-00 00:00:00' ;				
				$obj->approved = 0 ;				
				$obj->code = $vercode ;				
				$obj->point = $t_point ;				
				$obj->cash = $t_cash ;				
				$obj->point_id = $temp ;				
				$obj->status_of_redemption = 1;				
				$obj->status_of_redemption_date = date("Y-m-d H:i:s") ;				
				$obj->pick_up_date = '0000-00-00 00:00:00' ;				
				$obj->phone_num = '' ;			
				$obj->quantity = 1 ;								
				$db->insertObject('#__wrd_redemption', $obj) ;								
				
				//update quality				
				$query = 'UPDATE #__wrd_products SET quantity = '.($rs->quantity - 1).' WHERE id = '.$rs->id;						$db->setQuery($query);				
				$db->query();								
				
				//send mail							
				$mainframe->redirect('index.php?option=com_wrd&view=redemption&layout=finish&id='.$post['pid'].'&Itemid=127') ;				
				return false;			
			}else{
				$mainframe->redirect('index.php?option=com_wrd&view=redemtion&layout=products','Need more point');
				return false;			
								
			}					
			}
			else{
				if(!empty($post['pid'])){
									//get category of product				
									$model_product = $this->getModel('products') ;				
									$product_details = $model_product->getItem($post['pid']) ;				
									//redirect to products of category				
									$mainframe->redirect('index.php?option=com_wrd&view=redemption&layout=products&cid='.$product_details->cat_id.'&Itemid=125') ;			
				}				
			}	
		} 		

		function random_letters() {
		    $mainframe = JFactory::getApplication() ;				
		    $sample = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V','W','X','Y','Z');	    
		    $total = count($sample);	    
		    for ($i= 0; $i < 2; $i++) {
			    $rand = rand(0, $total-1) ;		    
			    $vercode = $vercode.$sample[$rand] ;    	
			}    	
			$vercode .= "".rand(1000,9999) ;    	
			return $vercode ;
		}	
}
				    			    
?>